﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Services.Description;
using YunShop_WebAPI.BLL;

namespace YunShop_WebAPI.API.Controllers
{
    public class GoodsController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string GetGoodsData(string mjson)
        {
            string msg = "";
            try
            {
                try
                {
                    Hashtable row = (Hashtable)PluSoft.Utils.JSON.Decode(mjson);
                    string sql = @"exec KingCloudShop_Interfaces.dbo.pr_bi_GetSysGoodsList @mPageSize,@mPageIndex,@mNumber,@mLastID";
                    Service.SqlParameterList par_master = new Service.SqlParameterList();
                    par_master.Add("@mPageSize", SqlDbType.Int, CommonFun.ConvertToInt(row["pageSize"]));
                    par_master.Add("@mPageIndex", SqlDbType.Int, CommonFun.ConvertToInt(row["pageIndex"]));
                    par_master.Add("@mNumber", SqlDbType.VarChar, "");
                    par_master.Add("@mLastID", SqlDbType.BigInt, CommonFun.ConvertToBigInt(row["lastId"]));
                    ArrayList data = DBHelper.Select(sql, par_master.Get_List(), DBHelper.ConnMaster);
                    string json = PluSoft.Utils.JSON.Encode(data);
                    return json;
                }
                catch (Exception E)
                {

                }
                return null;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
                msg = err.Message;
            }
            return null;
        }

        public string GetGoodsList()
        {
            Stream ReceiveStream = System.Web.HttpContext.Current.Request.GetBufferlessInputStream();
            StreamReader sr = new StreamReader(ReceiveStream);
            string mjson = sr.ReadToEnd();

            return GetGoodsData(mjson);
        }

        public string PostGoodsList([FromBody] string mjson)
        {
            return GetGoodsData(mjson);
        }

        public string GetGoodsList1()
        {
            int pageSize = CommonFun.ConvertToInt(HttpContext.Current.Request["pageSize"]);
            int pageIndex = CommonFun.ConvertToInt(HttpContext.Current.Request["pageIndex"]);
            long lastId = CommonFun.ConvertToBigInt(HttpContext.Current.Request["lastId"]);
            Hashtable ht = new Hashtable();
            ht["pageSize"] = pageSize;
            ht["pageIndex"] = pageIndex;
            ht["lastId"] = lastId;
            string mjson = PluSoft.Utils.JSON.Encode(ht);
            return GetGoodsData(mjson);
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}